package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingElderDto;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectSevice;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectSevice {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> page(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProject> nursingProjects = nursingProjectMapper.selectPage(name, status);
        PageResponse<NursingProjectVo> projectVoPageResponse = PageResponse.of(nursingProjects, NursingProjectVo.class);
        return projectVoPageResponse;

    }

    /**
     * 添加
     * @param nursingProjectDto
     */
    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto, nursingProject);
        nursingProjectMapper.insert(nursingProject);

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo findById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.findById(id);
        NursingProjectVo bean = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        return bean;
    }

    /**
     * 修改
     * @param nursingProjectDto
     */
    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto, nursingProject);
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProjectMapper.update(nursingProject);

    }

    /**
     * 启动禁用，1启用，0禁用
     * @param id
     * @param status
     */
    @Override
    public void status(Long id, Integer status) {
        nursingProjectMapper.status(id, status);
    }

    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public List<NursingProjectVo> findAll() {
        int count = 0;
        List<NursingProject> all = nursingProjectMapper.findAll();
        List<NursingProjectVo> nursingProjectVos = new ArrayList<>();
        for (NursingProject nursingProject : all) {
            NursingProjectVo nursingProjectVo = new NursingProjectVo();
            BeanUtils.copyProperties(nursingProject, nursingProjectVo);
            nursingProjectVos.add(nursingProjectVo);
            count++;
        }
        for (int i = 0; i < count; i++) {
            nursingProjectVos.get(i).setCount(count);
        }
        return nursingProjectVos;

    }

}
